package com.wzw.tcxb;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wzw.tcjh.CustomerTools;
import com.wzw.util.ProjectCode;

public class TCXBServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		
		String name = req.getParameter("name");
		String phone = req.getParameter("tel");
		String email = req.getParameter("email");
		String address = req.getParameter("address");
		String from = req.getParameter("from");
		
		System.out.println(ProjectCode.TCXB.getName() + "-表单来自于-" + from);
		System.out.println("Get one item of customer information: Name=" + name + ", Phone=" + phone + ", Email=" + email + ", Address="+address);
		
		if(null == name || name.equals("") || null == phone 
				|| phone.equals("") || null == email || email.equals("")) {
			resp.sendRedirect("http://www.hsbcjx-weibo.com/tcxb.jsp");
			return;
		}
		boolean addResult = false;
		try {
			addResult = CustomerTools.addCustomer(name, phone, email, address, ProjectCode.TCXB.getValue(), from);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(addResult) {
			System.out.println("Add a customer successfully!");
			resp.getWriter().write("success=yes");
		} else {
			System.out.println("Add a customer failed!");
			resp.getWriter().write("success=no");
		}
	}

}
